﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace BanHangCshap.Class
{
    class LoaiHang:Database
    {

        #region ***** Fields & Properties *****
        private string _maloai;
        public string maloai
        {
            get
            {
                return _maloai;
            }
            set
            {
                _maloai = value;
            }
        }
        private string _tenloai;
        public string tenloai
        {
            get
            {
                return _tenloai;
            }
            set
            {
                _tenloai = value;
            }
        }
        private double _chietkhau;
        public double chietkhau
        {
            get
            {
                return _chietkhau;
            }
            set
            {
                _chietkhau = value;
            }
        }
       
        private string _machungloai;
        public string machungloai
        {
            get
            {
                return _machungloai;
            }
            set
            {
                _machungloai = value;
            }
        }
        #endregion

        #region ***** Init Methods *****
        public LoaiHang()
        {
        }
        public LoaiHang(string maloai)
        {
            string sql = "select * from LoaiHang where maloai = '" + maloai + "'";
            foreach (DataRow dr in getDataTable(sql).Rows)
            {
                this.maloai = maloai;
                this.tenloai = dr["tenloai"].ToString().Trim();
                this.chietkhau = double.Parse(dr["chietkhau"].ToString());
                this.machungloai = dr["machungloai"].ToString().Trim();
            }
        }
        public LoaiHang(string maloai, string tenloai, double chietkhau, double thue, string machungloai)
        {
            this.maloai = maloai;
            this.tenloai = tenloai;
            this.chietkhau = chietkhau;
           
            this.machungloai = machungloai;
        }
        #endregion

        public DataTable layDSLoaiHang()
        {
            string sql = "select maloai,tenloai,chietkhau,a.machungloai from LoaiHang a, ChungLoaiHang b where a.machungloai = b.machungloai";
            return getDataTable(sql);
        }
        public Boolean themLoaiHang()
        {
            string sql = "insert into LoaiHang(maloai,tenloai,chietkhau, machungloai) values ('" + maloai + "',N'" + tenloai + "'," + chietkhau + ",'" + machungloai + "')";
            
            return executeQuery(sql);

        }
        public Boolean xoaLoaiHang(string maloai)
        {
            string sql = "delete LoaiHang where maloai = '" + maloai + "'";
            return executeQuery(sql);
        }
        public Boolean suaLoaiHang()
        {
            string sql = "update LoaiHang set tenloai = N'" + tenloai + "',chietkhau = " + chietkhau + ",machungloai = '" + machungloai + "' where maloai = '" + maloai + "'";
            return executeQuery(sql);
        }
        public DataTable layDSHangHoa(string maloai)
        {
            string sql = "select * from HangHoa where maloai = '" + maloai + "'";
            return getDataTable(sql);
        }
        public Boolean isExistLoaiHang(string maloai)
        {
            string sql = "select maloai from LoaiHang where maloai = '" + maloai + "'";
            return isExist(sql);
        }
        public string taoMaLoaiHang()
        {
            string sql = "select max(convert(int,maloai)) from LoaiHang";
            return string.Format("{0,3:000}", getNumberValue(sql) + 1);
        }

    }
}
